package com.mccken.designpattern.state;

import com.mccken.designpattern.state.sub.HasQuarterState;
import com.mccken.designpattern.state.sub.NoQuarterState;
import com.mccken.designpattern.state.sub.SoldOutState;
import com.mccken.designpattern.state.sub.SoldState;
import com.mccken.designpattern.state.sub.WinnerState;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 糖果机
 * 
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-02-26 15:50
 **/
@ToString
public class GumballMachine {
	@Getter
	private State soldOutState;
	@Getter
	private State noQuarterState;
	@Getter
	private State hasQuarterState;
	@Getter
	private State soldState;
	@Getter
	private State winnerState;
	
	@Getter
	@Setter
	private State state;
	@Getter
	private int count = 0;
	
	public GumballMachine(int numberGumballs) {
		soldOutState = new SoldOutState(this);
		noQuarterState = new NoQuarterState(this);
		hasQuarterState = new HasQuarterState(this);
		soldState = new SoldState(this);
		winnerState = new WinnerState(this);

		state = soldOutState;
		if (numberGumballs > 0) {
			count = numberGumballs;
			state = soldState;
		}
	}
	
	public void insertQuarter() {
		state.insertQuarter();
	}
	
	public void ejectQuarter() {
		state.ejectQuarter();
	}
	
	public void turnCrank() {
		state.turnCrank();
		state.dispense();
	}
	
	public void releaseBall() {
		System.out.println("A gumball comes rolling out the slot...");
		if (count != 0) {
			count = count - 1;
		}
	}

	public void refill(int count) {
		this.count = count;
		state = noQuarterState;
	}
	

	public static void main(String[] args) {
		GumballMachine gumballMachine = new GumballMachine(100);

		while (gumballMachine.getState() != gumballMachine.getSoldOutState()) {
			gumballMachine.insertQuarter();
			gumballMachine.turnCrank();	
		}

		System.out.println(0);
		
	}
	
}
